
/* $Log: Receivers.c,v $
/* Revision 1.4  2007/10/31 16:27:30  dimitrip
/* Add fews  command see CommandIterface.txt
/*
/* Revision 1.3  2006/12/17 17:00:08  dimitrip
/* *** empty log message ***
/*
/* Revision 1.2  2006/12/07 16:24:46  dimitrip
/* *** empty log message ***
/*
/* Revision 1.13  2006/12/03 10:18:39  dimitrip
/* Fix bugs.
/* Start of TELIT implementation.
/*
/* Revision 1.12  2006/11/15 09:06:42  dimitrip
/* Add routine of PIN PUK numbers.
/*
/* Revision 1.11  2006/10/02 21:42:03  Dima
/* Ready for multilanguage.
/*
/* Revision 1.10  2006/09/28 16:45:23  dimitrip
/* Add: remote control
/*
/* Revision 1.9  2006/09/17 19:27:07  dimitrip
/* Change RX receiver.
/* Cancel ECHO from modem.
/* Avaible to read SMS with CR.
/*
/* Revision 1.8  2006/09/14 21:26:54  dimitrip
/* Add: Aux_Port routine.
/* Fix fews bugs.
/*
/* Revision 1.7  2006/09/04 11:02:08  dimitrip
/* 	1. NMS Phone List and service routine for it. Maximum: 16 Phone Numbers of NMS.
/* 	2. IMSI Ranges. Maximum: 64.
/* 	3. Checking of SIM(internal) connection and validation(see IMSI range).
/* 	4. Two quickly pressed on the button '*' - shows '+' character in Dial mode.
/* 	5. Warning if the Card long time(10sec) still inserted.
/* 	6. Backlight of keyboard.
/* 	7. Changed item in STR__PHONE_SETTINGS menu - from STR__IMEI_NUMBER to new - STR__MODEM_DETAILS.
/* 	8. Change password option is aviable now (Phone Settings/Change Password).
/*
/* Revision 1.6  2006/08/01 16:55:02  dimitrip
/* Add:
/* 1. In Insataller Menu Delete EEPROM.
/* 2. Send Status.
/*
/* Revision 1.5  2006/06/26 18:07:09  dimitrip
/* Start of Firmware Update ( modem).
/*
/* Revision 1.4  2006/06/18 16:31:32  dimitrip
/* DATA MODE
/*
/* Revision 1.3  2006/05/30 19:55:24  dimitrip
/* Command Interface
/*
/* Revision 1.2  2006/05/16 18:03:44  dimitrip
/* SMS Control development. Need: save New SMS of
/* user(numbers only) into internal EEPROM.
/*
/* Revision 1.1  2006/05/10 11:11:50  dimitrip
/* SMS development: Receive SMS mechanism
/*
/* Revision 1.1  2006/04/20 08:33:19  dimitrip
/* *** empty log message ***
/*
/* Revision 1.7  2006/04/06 17:22:02  dimitrip
/* Changing Prefix format from 4 digit. to 6.
/* */

#include "main.h"

enum
{
	REC_SYNC_CR = 0,
	REC_SYNC_LF,
	REC_BUFFER_OVERFLOW,
	RECEIVE_BUFFER_OVERFLOW
};

enum
{
	REC_RESET = 0,
	GET_PACKET_LENGTH_LOW,
	GET_PACKET_LENGTH_HIGH,
 	BYTES_RECEIVE,
};

//volatile unsigned char near Escape = 0;				 		 /* over from DATA mode to COMMAND mode */

volatile near bit RECEIVER = MODEM_RX_1;
unsigned char near ModemEvents = 0;
unsigned char near AuxPortEvents = 0;
volatile unsigned char near CheckSum = 0;
volatile unsigned int near BytesCount = 0;
//unsigned int near ReceiverBytesCount = 0;
unsigned char ModemRecBuffer[MODEM_REC_BUFFER_SIZE];
unsigned char Mod_Rec_Buff[MODEM_REC_BUFFER_SIZE];
volatile unsigned char near pModemRecBuffer = 0;

unsigned char AuxPortRecBuffer[AUX_PORT_REC_BUFFER_SIZE];
volatile unsigned char AuxPort_Rec_Buffer[AUX_PORT_REC_BUFFER_SIZE];
volatile near unsigned char pAuxPortRecBuffer = 0;
volatile near bit AUX_PORT_REC_COMPLETE = 0;

volatile near unsigned char ModemReceiveStatus = REC_SYNC_CR;
//volatile unsigned char RecByteIndicator = 0;

void Aux_Port_RX ( void )
{
	unsigned char Buffer = RCREG2;
	
	if ( Buffer == '\r' )  
	{
		AuxPort_Rec_Buffer[pAuxPortRecBuffer] = 0;
		pAuxPortRecBuffer = 0;
		AUX_PORT_REC_COMPLETE = TRUE;
	}
	else
	{
		AuxPort_Rec_Buffer[pAuxPortRecBuffer] = Buffer;
		if ( ++pAuxPortRecBuffer >= AUX_PORT_REC_BUFFER_SIZE )
			pAuxPortRecBuffer = 0;
	}
}

void Modem_RX_1( void )
{
	//unsigned char PrevBuffer;
	unsigned char Buffer = RCREG1;
	
	switch ( ModemReceiveStatus )
	{
		//case REC_COMPLETE:
		case REC_SYNC_CR:
			if ( Buffer == '\r' )  
			{
				ModemReceiveStatus = REC_SYNC_LF;
			}
			else
			{
				Mod_Rec_Buff[pModemRecBuffer++] = Buffer;
			}
			break;

		case REC_SYNC_LF:
			if ( Buffer == '\n' )
			{	
				if ( pModemRecBuffer > 0 )
				{
					RTS = 1;			  /* Receive complete */
					Mod_Rec_Buff[pModemRecBuffer] = 0; /* end of string */
				}
		
				pModemRecBuffer = 0;
				ModemReceiveStatus = REC_SYNC_CR;
			}
			
			//pModemRecBuffer = 0;
			//ModemReceiveStatus = REC_SYNC_CR;
			break;

/* 		case REC_BUFFER_OVERFLOW:	 */
			
/* 			break;	 */
			
		default:
			;
	}
}

void Modem_RX_2( void )
{
	unsigned char Buffer = RCREG1;

	switch ( ModemReceiveStatus )
	{
		case REC_RESET:
			CheckSum = 0;
			pModemRecBuffer = 0;
			BytesCount = (unsigned int)Buffer; /* get low byte of packet length */
			ModemReceiveStatus = GET_PACKET_LENGTH_HIGH;
			break; 

		case GET_PACKET_LENGTH_HIGH:
			BytesCount |= (unsigned int)Buffer << 8; 
			ModemReceiveStatus = BYTES_RECEIVE;
			break;

		case BYTES_RECEIVE:
			Mod_Rec_Buff[pModemRecBuffer] = Buffer;
			if ( --BytesCount == 0 )
			{
				RTS = 1;				  /* Stop Receive */
				ModemReceiveStatus = REC_RESET;
			}
			else
			{
				CheckSum ^= Mod_Rec_Buff[pModemRecBuffer++];
				break;
			}

		default :
			; 
	}
}

void Drive_Receivers( void )
{
	ModemEvents = 0;
	AuxPortEvents = 0;

	if ( RTS )
	{
		if ( RECEIVER == MODEM_RX_1 )	/* Normal Mode */
		{
			if ( StrCmp(STR__OK, Mod_Rec_Buff) == 0 )
			{
				ModemEvents = MODEM_NEW_MESSAGE_COMPLETE;
			}
			else
				if ( StrCmp("ERROR", Mod_Rec_Buff) == 0 )
				{
					ModemEvents = MODEM_ERROR;
				}
				else            
					if ( StrCmp("NO CARRIER", Mod_Rec_Buff) == 0 )
					{
						ModemEvents = NO_CARRIER;
					}
					else
						if ( !StrCmp("CONNECT", Mod_Rec_Buff) || errno == 7 )	/* TELIT return - "CONNECT 9600" */
						{
							ModemEvents = DATA_MODE_CONNECT;
						} 
						else
							if ( StrCmp("BUSY", Mod_Rec_Buff)  == 0 )
							{
								ModemEvents = MODEM_RESPONSE_BUSY;
							}
//#ifdef	MODEM_TYPE__TELIT
							else
								if ( StrCmp("+CMS ERROR", Mod_Rec_Buff) == 0 || errno == 10 )
								{
									ModemEvents = MODEM_ERROR;
								}
//#endif
								else
								{
									StrCpy( ModemRecBuffer, Mod_Rec_Buff );
									ModemEvents = MODEM_NEW_MESSAGE_PART;
									dbg("\n<< [%s]\n",ModemRecBuffer); //Dima
								}
		}
		else
			if ( RECEIVER == MODEM_RX_2 )	/* Data Mode */
			{
				if ( CheckSum == Mod_Rec_Buff[pModemRecBuffer] )
				{
					Mod_Rec_Buff[pModemRecBuffer] = 0; 				/* end of string */
					ModemEvents = DATA_MODE_RECEIVE_COMPLETE;
					/* No need CheckSum and Reserve Byte*/
					MemCpy( ModemRecBuffer, Mod_Rec_Buff+1, pModemRecBuffer ); //'+1' - Reserve byte
					//ReceiverBytesCount = pModemRecBuffer; 
				}
				else
				{
					ModemEvents = DATA_MODE_CHECKSUM_ERRORS;
				}
			}

		RTS = 0;
	}

	if ( AUX_PORT_REC_COMPLETE )
	{
		//BEEP();
		StrCpy(AuxPortRecBuffer, (unsigned char*)AuxPort_Rec_Buffer);
		
		Debug("\rAuxPort: %s\r", AuxPortRecBuffer);

		AUX_PORT_REC_COMPLETE = FALSE;
		AuxPortEvents = AUX_PORT_RECEIVE_COMPLETE;
	}
}
 
void ReceiverReset( void )
{
	ModemReceiveStatus = 0;
}

/* void Drive_Receivers_Test( void ) */
/* { */
/* 	static unsigned char bank1 cnt = 0; */
/* 	for(;;) */
/* 	{ */
/* //		SysTime(); */
/* 		Drive_Receivers(); */
		
/* 		if ( ModemEvents == MODEM_NEW_MESSAGE_COMPLETE ) */
/* 		{ */
/* 			if ( cnt == 4 ) */
/* 			{ */
/* 				cnt = 0; */
/* 				lcd_clear(); */
/* 			} */

/* 			lcd_goto(0, cnt++); */
/* 			lcd_puts(ModemRecBuffer); */
/* 		} */
/* 	} */
/* } */



/* 		case REC_BUSY: */
/* 			if ( Buffer == '\r' ) */
/* 			{ */
/* 				ModemReceiveStatus = REC_SYNC_LF; */
/* 				Mod_Rec_Buff[pModemRecBuffer] = 0; */
/* 				return;  */
/* 			} */
			
/* 			Mod_Rec_Buff[pModemRecBuffer] = Buffer; */
			
/* 				/\* Buffer overflow?? *\/ */
/* 			if ( ++pModemRecBuffer == MODEM_REC_BUFFER_SIZE ) */
/* 			{ */
/* 				ModemReceiveStatus = REC_BUFFER_OVERFLOW; */
/* 			} */
/* 			break; */


/* 	if ( Buffer == '+' ) 			/\* "+++" *\/ */
/* 	{ */
/* 		if ( ++Escape == 3 )			/\* "+++" - exit from DATA mode to COMMAND mode *\/ */
/* 		{ */
/* 			RTS = 1;					   */
/* 			ModemReceiveStatus = REC_RESET; */
/* 			return ; */
/* 		} */
/* 	} */
/* 	else */
/* 	{ */
/* 		Escape = 0; */
/* 	} */

/* 		if ( Escape == 3 )			/\* exit from DATA mode to COMMAND mode *\/ */
/* 				{ */
/* 					Escape = 0; */
/* 					ModemEvents = DATA_MODE_ESCAPE; */
/* 				} */
/* 				else */
